/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package educar.abmCursa;

import educar.models.Cargos;
import educar.models.Cursa;
import educar.models.Docente;
import educar.models.Facultad;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author Matias
 */
public class ModificarCursaController implements ActionListener {
    private AgregarCursarView agregarCursaView;
    private String vector[] = new String[2];    
    private CursaController cursaControl;
    private int fila;
    private String keys[] = new String[2]; 
    
// <editor-fold defaultstate="collapsed" desc="Constructor">
    public ModificarCursaController(CursaController CursaControler){        
        cursaControl = CursaControler;
        agregarCursaView = new AgregarCursarView();
        agregarCursaView.setActionListener(this);
    }
 // </editor-fold>  

// <editor-fold defaultstate="collapsed" desc="Action setView">    
    public void setView(Vector registro, int fil) {
        fila = fil;
        keys[0] = (String) registro.elementAt(1);
        keys[1] = (String) registro.elementAt(2);  
        agregarCursaView.setTitulo("Modificar un Cursado");
        agregarCursaView.setCodMateria((String) registro.elementAt(1));
        agregarCursaView.setnroAlumno((String) registro.elementAt(2));
        agregarCursaView.setVisible(true);
    }
// </editor-fold>      

// <editor-fold defaultstate="collapsed" desc="Metodos ActionPermormed">     
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (agregarCursaView.getJbCancelar() == source){
            agregarCursaView.dispose();
        }
        if (agregarCursaView.getJbAceptar() == source){
            vector[0] = agregarCursaView.getCodMateria(); 
            vector[1] = agregarCursaView.getnroAlumno();
           if (!((vector[0].length() == 0) || (vector[1].length() == 0))) {
                if ((Docente.exist(vector[0])) && (Facultad.exist(vector[1])) && (Cargos.exist(vector[2]))) {
                    Cursa cursa = new Cursa(vector[0],vector [1]);                
                    if (cursa.setCursaByKeys(keys[0], keys[1])) {                             
                               cursaControl.actualizarFila(vector, fila);
                              agregarCursaView.dispose();
                    } else {
                     JOptionPane.showMessageDialog(null,"Ya existe un registro con el codigoDeMateria "+ vector[0]+" , y el NumeroDeAlumno"+ vector[1]+".","proyecto 2012",JOptionPane.WARNING_MESSAGE);
                    }
                } else{
                    JOptionPane.showMessageDialog(null,"Alguno de los campos no existe","proyecto 2012",JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null,"Existe al menos un campo sin completar.\n\nRevise los campos ingresados","proyecto 2012",JOptionPane.WARNING_MESSAGE);
            }
        }
    }
// </editor-fold>
}
